package com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.RepayInfo_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoanFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.RepayPaystackActivity_Niri_wooden;


public class RepayFragment_wooden extends BaseLoanFragment_wooden {

    private TextView totalRepaymentTv_wooden, dueDateTv_wooden;
    private TextView loanAmountTv_wooden, interestTv_wooden, serviceFeeTv_wooden, overdueDaysTv_wooden, overduePenaltyTv_wooden;

    public boolean isOverdue = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repay_wooden, container, false);
        init_wooden(view);
        return view;
    }

    private void init_wooden(View view){

        totalRepaymentTv_wooden = view.findViewById(R.id.fragment_repay_tv_total_repayment);
        dueDateTv_wooden = view.findViewById(R.id.fragment_repay_tv_due_date);
        loanAmountTv_wooden = view.findViewById(R.id.fragment_repay_tv_loan_amount);
        interestTv_wooden = view.findViewById(R.id.fragment_repay_tv_interest);
        serviceFeeTv_wooden = view.findViewById(R.id.fragment_repay_tv_service_fee);
        overdueDaysTv_wooden = view.findViewById(R.id.fragment_repay_tv_overdue_days);
        overduePenaltyTv_wooden = view.findViewById(R.id.fragment_repay_tv_overdue_penalty);

        view.findViewById(R.id.fragment_repay_btn_query_process).setOnClickListener(v -> HttpManager_wooden.getInstance_wooden().requestUserProcess_wooden());
        view.findViewById(R.id.fragment_repay_btn_repay).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPointAndUpload((this.getClass().getSimpleName()+"_CLICK").toUpperCase());

            switch (StaticVariate_wooden.Select_Country_wooden){
                case Constant_wooden.Country_Cote_wooden:
                case Constant_wooden.Country_Ghana_wooden:
                case Constant_wooden.Country_Vietnam_wooden:
                    HttpManager_wooden.getInstance_wooden().orderRepayH5_gh_cl_vi_wooden();
                    break;
                case Constant_wooden.Country_Uganda_wooden:
                case Constant_wooden.Country_Kenya_wooden:
                    HttpManager_wooden.getInstance_wooden().orderRepayH5_ug_ke_wooden();
                    break;
                case Constant_wooden.Country_Nigeria_wooden:
                    HttpManager_wooden.getInstance_wooden().bankList_niri_wooden();
                    break;
            }
        });

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        HttpManager_wooden.getInstance_wooden().getRepayDetail_wooden();
    }

    @SuppressLint("SetTextI18n")
    public void updateRepayDetail_wooden(){

        //货币单位
        String moneyUnit = StringUtil_wooden.getSafeString_wooden(StaticVariate_wooden.getMoneyUnit_wooden()).toUpperCase();

        totalRepaymentTv_wooden.setText(moneyUnit+" "+ RepayInfo_wooden.getInstance_wooden().getRepayAmount_wooden());
        dueDateTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getRepayDate_wooden());
        loanAmountTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getAmount_wooden()+" "+moneyUnit);
        interestTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getInterest_wooden()+" "+moneyUnit);
        overdueDaysTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getOverdueDay_wooden()+" "+getString(R.string.word_days));
        overduePenaltyTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getOverdueAmount_wooden()+" "+moneyUnit);

        //判断是否逾期
        if (RepayInfo_wooden.getInstance_wooden().isOverdue_wooden()){
            isOverdue = true;
            overdueDaysTv_wooden.setTextColor(Color.RED);
        }else {
            isOverdue = false;
            overdueDaysTv_wooden.setTextColor(requireContext().getColor(R.color.blue_wooden));
        }

        switch (StaticVariate_wooden.Select_Country_wooden){
            case Constant_wooden.Country_Cote_wooden:
                serviceFeeTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getRepayProFee_wooden()+" "+moneyUnit);
                HttpManager_wooden.getInstance_wooden().fetchBoundBankAccount_Cote_wooden();
                break;
            default:
                serviceFeeTv_wooden.setText(RepayInfo_wooden.getInstance_wooden().getServiceFee()+" "+moneyUnit);
        }
    }

}